<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
    <!-- table -->
    <!-- 旧件返件类型维护 - 旧件返件类型维护编辑 -->
    <FormPanel ref="formPanel1" :list="formList" :cols="1" :formType="'edit'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="margin-bottom: 1rem;"></div>
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="edit()">添加</multiuse-button>
      </template>
    <div style="padding-top: 1rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <Vehicle @close="close" @save="save"></Vehicle>
    </BaseDialog>
    </Spin>
  </div>
</template>
<script>
import Vehicle from '@/pages/claimDemage/tools/vehicle.vue';
import { copyUpdateData } from '@/utils/index';
import { getPartType } from '@/api/dictionary/index';
import { editSave, selectById } from '@/api/claim/factory/OldPartReturnTypeMaintain';
import { getAppId} from '@/assets/js/auth';
import {dictionary} from'@/utils/dictMixin';
export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],
  // mixins: [dictionary],
  components: {
    Vehicle
  },
  data() {
    return {
      loading: false,
      dialogTitle: '配件选择',
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      saveData: null,
      editData: null,
      newVar: {}
    };
  },
  created() {
    this.init();
    if(getAppId()=='beijing'){
      this.formList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
    }else{
      this.formList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
      this.formList.find((x) => x.fieldName == 'powerType').initialValue = 60081002;
      this.formList.find((x) => x.fieldName == 'powerType').disabled=true;
    }
  },
  mounted() {

  },
  methods: {
    async init() {
      this.loading = true;
      const res = await getPartType({ type: 5585 });
      if (res.resultCode == 200) {
        // this.formList.find(x => x.fieldName == 'typeId').initialValue = '';
        this.formList.find(x => x.fieldName == 'typeId').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }

      let newVar = await selectById({id : this.rowData.returnId});
      this.editData = newVar.data;
      this.formList.forEach(x => {
        console.log(newVar);
        x.initialValue = newVar.data[x.fieldName];
      });
      this.newVar = newVar.data;
      this.loading = false;

    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.saveData !== null) {
        console.log(this.saveData);
        // this.saveData.returnId= this.editData.returnId
        // this.saveData.isDeleted= this.editData.isDeleted
        // this.saveData.createdBy = this.editData.createdBy
        // this.saveData.createdAt = this.editData.createdAt
        // this.saveData.appID= this.editData.appID
        this.newVar = copyUpdateData(this.newVar,this.saveData);
        this.loading = true;
        let res = await editSave(this.newVar);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
          // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          // this.closeDialog();
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    },
    chooseOrderNo() {
      this.visible = true;
    },
    close(){
      this.visible = false;
    },
    save(obj) {
      this.formList.find(x => x.fieldName == 'partCode').initialValue = obj[0].partCode;
      this.formList.find(x => x.fieldName == 'partName').initialValue = obj[0].partName;
      this.visible = false;
    },
    edit() {
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '返件代码',
          fieldName: 'partCode',
          placeholder: '请选择返件代码...',
         readonly: true, // 只读
          rules: [{required: true, message: '请选择返件代码...', trigger: 'change'}],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()}/>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '返件名称',
          fieldName: 'partName',
          rules: [{required: true, message: '请输入返件名称...', trigger: 'change'}],
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '返件类型',
          fieldName: 'typeId',
          filterable: true,
          placeholder: '请选择返件类型...',
          itemList: [],
          rules: [{required: true, message: '请选择返件类型...', trigger: 'change'}],
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择动力形式...',
          initialValue: '',
          itemList: this.createDictList('6008'),
          filterable:true,
          rules: [{required: true, message: '请选择动力形式...', trigger: 'change'}]
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'typeStatus',
          filterable: true,
          placeholder: '请选择状态...',
          itemList: [{text: '启用', value: 1}, {text: '禁用', value: 0}],
          rules: [{required: true, message: '请选择状态...', trigger: 'change'}],
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
